package com.exam.web.action.privilege;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.exam.bean.PageView;
import com.exam.bean.privilege.PrivilegeGroup;
import com.exam.bean.privilege.SystemPrivilege;
import com.exam.bean.privilege.SystemPrivilegePK;
import com.exam.service.privilege.PrivilegeGroupService;
import com.exam.service.privilege.SystemPrivilegeService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller("privilegeGroupAction")
@Scope("prototype")
public class PrivilegeGroupAction extends ActionSupport {
	
	private static final long serialVersionUID = -3391698687507041322L;

	@Resource
	private PrivilegeGroupService privilegeGroupService;
	@Resource
	private SystemPrivilegeService systemPrivilegeService;
	
	private int page;//分页
	private String query;//查询
	private PageView<PrivilegeGroup> pageView;//记录集
	private PrivilegeGroup privilegegroup;//权限组
	private Integer groupid;//id
	private Integer[] groupids;//ids
	private List<SystemPrivilege> privileges;
	private List<SystemPrivilegePK> privilegePKs;
	private Set<SystemPrivilege> selectprivileges;
	
	/**
	 * 权限组列表显示
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="privilegegroup", privilegeValue="view")
	public String list()throws Exception {
		PageView<PrivilegeGroup> pageView = new PageView<PrivilegeGroup>(12, this.getPage());
		int firstindex = (pageView.getCurrentPage()-1)* pageView.getMaxResult();
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("id", "desc");
		
		StringBuffer jpql=new StringBuffer("");
		List<Object> params=new ArrayList<Object>();
		if("true".equals(this.getQuery())){
			if(this.getPrivilegegroup().getName()!=null&&""!=this.getPrivilegegroup().getName().trim()){
				jpql.append("o.name like ?"+ (params.size()+1));
				params.add("%"+ this.getPrivilegegroup().getName().trim()+ "%");
				pageView.setQueryResult(privilegeGroupService.getScrollData(PrivilegeGroup.class, firstindex, 
						pageView.getMaxResult(),jpql.toString(),params.toArray(), orderby));
				this.setPageView(pageView);
				return "success";
			}
		}
		pageView.setQueryResult(privilegeGroupService.getScrollData(PrivilegeGroup.class, firstindex, 
				pageView.getMaxResult(), orderby));
		this.setPageView(pageView);
		return "success";
	}
	
	/**
	 * 权限组查看界面
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="privilegegroup", privilegeValue="view")
	public String viewUI() throws Exception {
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("description", "desc");
		this.setPrivileges(systemPrivilegeService.getScrollData(SystemPrivilege.class,orderby).getResultList());
		PrivilegeGroup privilegeGroup = privilegeGroupService.find(PrivilegeGroup.class, this.groupid);
		this.setPrivilegegroup(privilegeGroup);
		this.setSelectprivileges(privilegeGroup.getPrivileges());
		return "view";
	}
	
	/**
	 * 权限组添加界面
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="privilegegroup", privilegeValue="insert")
	public String addUI() throws Exception {
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("description", "desc");
		this.setPrivileges(systemPrivilegeService.getScrollData(SystemPrivilege.class,orderby).getResultList());
		return "add";
	}
	
	/**
	 * 权限组添加
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="privilegegroup", privilegeValue="insert")
	public String add() throws Exception {
		PrivilegeGroup privilegeGroup = this.getPrivilegegroup();
		if(null!=this.getPrivilegePKs()&&this.getPrivilegePKs().size()>0){
			for(SystemPrivilegePK pk : this.getPrivilegePKs()){
				privilegeGroup.addPrivilege(new SystemPrivilege(pk));
			}
		}
		privilegeGroupService.save(privilegeGroup);
		ActionContext.getContext().put("message", "添加用户权限组成功");
		ActionContext.getContext().put("urladdress", "privilegegroup_list.html");
		return "message";
	}
	
	/**
	 * 权限组修改界面
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="privilegegroup", privilegeValue="update")
	public String editUI() throws Exception {
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("description", "desc");
		this.setPrivileges(systemPrivilegeService.getScrollData(SystemPrivilege.class,orderby).getResultList());
		PrivilegeGroup privilegeGroup = privilegeGroupService.find(PrivilegeGroup.class, this.groupid);
		this.setPrivilegegroup(privilegeGroup);
		this.setSelectprivileges(privilegeGroup.getPrivileges());
		return "edit";
	}
	
	/**
	 * 权限组修改
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="privilegegroup", privilegeValue="update")
	public String edit() throws Exception {
		PrivilegeGroup privilegeGroup = privilegeGroupService.find(PrivilegeGroup.class, this.getGroupid());
		privilegeGroup.setName(this.getPrivilegegroup().getName());
		
		List<SystemPrivilege> deletes = new ArrayList<SystemPrivilege>();
		List<SystemPrivilegePK> pks = this.getPrivilegePKs();
		if(null!=pks&&pks.size()>0){
			for(SystemPrivilege p : privilegeGroup.getPrivileges()){
				if(!pks.contains(p.getId())) deletes.add(p);
			}
			privilegeGroup.getPrivileges().removeAll(deletes);
			for(SystemPrivilegePK pk : pks){
				SystemPrivilege systemPrivilege = new SystemPrivilege(pk);
				if(!privilegeGroup.getPrivileges().contains(systemPrivilege)){
					privilegeGroup.addPrivilege(systemPrivilege);
				}
			}
		}
		else{
			privilegeGroup.getPrivileges().clear();
		}
		privilegeGroupService.update(privilegeGroup);
		ActionContext.getContext().put("message", "修改用户权限组成功");
		ActionContext.getContext().put("urladdress", "privilegegroup_list.html");
		return "message";
	}
	
	/**
	 * 权限组删除
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="privilegegroup", privilegeValue="delete")
	public String deleteAll() throws Exception {
		privilegeGroupService.delete(PrivilegeGroup.class, this.getGroupids());
		ActionContext.getContext().put("message", "删除用户权限组成功");
		ActionContext.getContext().put("urladdress", "privilegegroup_list.html");
		return "message";
	}
	
	/**
	 * 权限组删除
	 * @return
	 * @throws Exception
	 */
	@Privilege(model="privilegegroup", privilegeValue="delete")
	public String delete() throws Exception {
		privilegeGroupService.delete(PrivilegeGroup.class, this.getGroupid());
		ActionContext.getContext().put("message", "删除用户权限组成功");
		ActionContext.getContext().put("urladdress", "privilegegroup_list.html");
		return "message";
	}

	public int getPage() {
		return page<1?1:page;
	}
	public void setPage(int page) {
		this.page = page;
	}

	public String getQuery() {
		return query;
	}
	public void setQuery(String query) {
		this.query = query;
	}

	public PageView<PrivilegeGroup> getPageView() {
		return pageView;
	}
	public void setPageView(PageView<PrivilegeGroup> pageView) {
		this.pageView = pageView;
	}

	public PrivilegeGroup getPrivilegegroup() {
		return privilegegroup;
	}
	public void setPrivilegegroup(PrivilegeGroup privilegegroup) {
		this.privilegegroup = privilegegroup;
	}

	public Integer getGroupid() {
		return groupid;
	}
	public void setGroupid(Integer groupid) {
		this.groupid = groupid;
	}

	public Integer[] getGroupids() {
		return groupids;
	}
	public void setGroupids(Integer[] groupids) {
		this.groupids = groupids;
	}

	public List<SystemPrivilege> getPrivileges() {
		return privileges;
	}
	public void setPrivileges(List<SystemPrivilege> privileges) {
		this.privileges = privileges;
	}

	public List<SystemPrivilegePK> getPrivilegePKs() {
		return privilegePKs;
	}
	public void setPrivilegePKs(List<SystemPrivilegePK> privilegePKs) {
		this.privilegePKs = privilegePKs;
	}

	public Set<SystemPrivilege> getSelectprivileges() {
		return selectprivileges;
	}
	public void setSelectprivileges(Set<SystemPrivilege> selectprivileges) {
		this.selectprivileges = selectprivileges;
	}
	
	
}
